/*
 * PortletTipoDestinoPage.java
 *
 * Created on January 8, 2008, 4:37 PM
 * Copyright FSanchez
 */
package gestionportletadmintipodestino;

import com.computec.admintipodestinocliente.delegate.AdminTipoDestinoClienteDelegate;
import com.computec.admintipodestinocliente.exception.TipoDestinoClienteException;
import com.computec.admintipodestinocliente.source.AdminTipoDestinoSource;
import com.src.computec.radar.services.utilities.consultas.delegate.ConsultasDelegate;
import com.src.computec.radar.services.utilities.consultas.exception.ConsultasException;
import com.src.computec.radar.services.utilities.dto.DepartamentoDto;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import java.util.ArrayList;
import java.util.Iterator;
import javax.faces.FacesException;
import com.sun.rave.web.ui.component.Form;
import com.sun.rave.web.ui.component.ThemeLinks;
import javax.faces.component.html.HtmlOutputText;
import com.sun.rave.web.ui.component.DropDown;
import java.util.List;
import javax.faces.event.ValueChangeEvent;
import javax.faces.component.html.HtmlPanelGrid;
import com.sun.rave.web.ui.model.SingleSelectOptionsList;
import javax.faces.component.html.HtmlCommandButton;
import javax.faces.component.html.HtmlDataTable;
import com.sun.jsfcl.data.DefaultTableDataModel;
import javax.faces.component.UIColumn;
import com.sun.rave.web.ui.component.Checkbox;
import javax.faces.component.html.HtmlPanelGroup;
import com.sun.rave.web.ui.component.Alert;


public class PortletTipoDestinoPage extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Definition">
    
    private int __placeholder;
    
    private ThemeLinks themeLinks1 = new ThemeLinks();
    
    public ThemeLinks getThemeLinks1() {
	return themeLinks1;
    }
    
    public void setThemeLinks1(ThemeLinks t) {
	this.themeLinks1 = t;
    }
    
    private Form form1 = new Form();
    
    public Form getForm1() {
	return form1;
    }
    
    public void setForm1(Form f) {
	this.form1 = f;
    }

    private HtmlOutputText outputText1 = new HtmlOutputText();

    public HtmlOutputText getOutputText1() {
        return outputText1;
    }

    public void setOutputText1(HtmlOutputText hot) {
        this.outputText1 = hot;
    }

    private DropDown dropDownCliente = new DropDown();

    public DropDown getDropDownCliente() {
        return dropDownCliente;
    }

    public void setDropDownCliente(DropDown dd) {
        this.dropDownCliente = dd;
    }

    private HtmlOutputText outputText2 = new HtmlOutputText();

    public HtmlOutputText getOutputText2() {
        return outputText2;
    }

    public void setOutputText2(HtmlOutputText hot) {
        this.outputText2 = hot;
    }

    private DropDown dropDownTipodestinoCliente = new DropDown();

    public DropDown getDropDownTipodestinoCliente() {
        return dropDownTipodestinoCliente;
    }

    public void setDropDownTipodestinoCliente(DropDown dd) {
        this.dropDownTipodestinoCliente = dd;
    }

    private HtmlPanelGrid gridPanelContainer = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelContainer() {
        return gridPanelContainer;
    }

    public void setGridPanelContainer(HtmlPanelGrid hpg) {
        this.gridPanelContainer = hpg;
    }

    private HtmlPanelGrid gridPanelTop = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelTop() {
        return gridPanelTop;
    }

    public void setGridPanelTop(HtmlPanelGrid hpg) {
        this.gridPanelTop = hpg;
    }

    private HtmlPanelGrid gridPanelBottom = new HtmlPanelGrid();

    public HtmlPanelGrid getGridPanelBottom() {
        return gridPanelBottom;
    }

    public void setGridPanelBottom(HtmlPanelGrid hpg) {
        this.gridPanelBottom = hpg;
    }

    private HtmlOutputText outputText3 = new HtmlOutputText();

    public HtmlOutputText getOutputText3() {
        return outputText3;
    }

    public void setOutputText3(HtmlOutputText hot) {
        this.outputText3 = hot;
    }

    private DropDown dropDownDepartamento = new DropDown();

    public DropDown getDropDownDepartamento() {
        return dropDownDepartamento;
    }

    public void setDropDownDepartamento(DropDown dd) {
        this.dropDownDepartamento = dd;
    }

    private HtmlOutputText outputText4 = new HtmlOutputText();

    public HtmlOutputText getOutputText4() {
        return outputText4;
    }

    public void setOutputText4(HtmlOutputText hot) {
        this.outputText4 = hot;
    }

    private DropDown dropDownMunicipio = new DropDown();

    public DropDown getDropDownMunicipio() {
        return dropDownMunicipio;
    }

    public void setDropDownMunicipio(DropDown dd) {
        this.dropDownMunicipio = dd;
    }

    private HtmlCommandButton buttonConsultar = new HtmlCommandButton();

    public HtmlCommandButton getButtonConsultar() {
        return buttonConsultar;
    }

    public void setButtonConsultar(HtmlCommandButton hcb) {
        this.buttonConsultar = hcb;
    }

    private HtmlDataTable dataTableMatrizTDC = new HtmlDataTable();

    public HtmlDataTable getDataTableMatrizTDC() {
        return dataTableMatrizTDC;
    }

    public void setDataTableMatrizTDC(HtmlDataTable hdt) {
        this.dataTableMatrizTDC = hdt;
    }

    private UIColumn columnDepartamento = new UIColumn();

    public UIColumn getColumnDepartamento() {
        return columnDepartamento;
    }

    public void setColumnDepartamento(UIColumn uic) {
        this.columnDepartamento = uic;
    }

    private HtmlOutputText outputText5 = new HtmlOutputText();

    public HtmlOutputText getOutputText5() {
        return outputText5;
    }

    public void setOutputText5(HtmlOutputText hot) {
        this.outputText5 = hot;
    }

    private HtmlOutputText outputText6 = new HtmlOutputText();

    public HtmlOutputText getOutputText6() {
        return outputText6;
    }

    public void setOutputText6(HtmlOutputText hot) {
        this.outputText6 = hot;
    }

    private UIColumn columnMunicipio = new UIColumn();

    public UIColumn getColumnMunicipio() {
        return columnMunicipio;
    }

    public void setColumnMunicipio(UIColumn uic) {
        this.columnMunicipio = uic;
    }

    private HtmlOutputText outputText7 = new HtmlOutputText();

    public HtmlOutputText getOutputText7() {
        return outputText7;
    }

    public void setOutputText7(HtmlOutputText hot) {
        this.outputText7 = hot;
    }

    private HtmlOutputText outputText8 = new HtmlOutputText();

    public HtmlOutputText getOutputText8() {
        return outputText8;
    }

    public void setOutputText8(HtmlOutputText hot) {
        this.outputText8 = hot;
    }

    private UIColumn columnCenrtoP = new UIColumn();

    public UIColumn getColumnCenrtoP() {
        return columnCenrtoP;
    }

    public void setColumnCenrtoP(UIColumn uic) {
        this.columnCenrtoP = uic;
    }

    private HtmlOutputText outputText9 = new HtmlOutputText();

    public HtmlOutputText getOutputText9() {
        return outputText9;
    }

    public void setOutputText9(HtmlOutputText hot) {
        this.outputText9 = hot;
    }

    private HtmlOutputText outputText10 = new HtmlOutputText();

    public HtmlOutputText getOutputText10() {
        return outputText10;
    }

    public void setOutputText10(HtmlOutputText hot) {
        this.outputText10 = hot;
    }

    private UIColumn columnTipoDestino = new UIColumn();

    public UIColumn getColumnTipoDestino() {
        return columnTipoDestino;
    }

    public void setColumnTipoDestino(UIColumn uic) {
        this.columnTipoDestino = uic;
    }

    private HtmlOutputText outputText11 = new HtmlOutputText();

    public HtmlOutputText getOutputText11() {
        return outputText11;
    }

    public void setOutputText11(HtmlOutputText hot) {
        this.outputText11 = hot;
    }

    private HtmlOutputText outputText12 = new HtmlOutputText();

    public HtmlOutputText getOutputText12() {
        return outputText12;
    }

    public void setOutputText12(HtmlOutputText hot) {
        this.outputText12 = hot;
    }

    private Checkbox checkboxTDC = new Checkbox();

    public Checkbox getCheckboxTDC() {
        return checkboxTDC;
    }

    public void setCheckboxTDC(Checkbox c) {
        this.checkboxTDC = c;
    }

    private HtmlCommandButton buttonAsignar = new HtmlCommandButton();

    public HtmlCommandButton getButtonAsignar() {
        return buttonAsignar;
    }

    public void setButtonAsignar(HtmlCommandButton hcb) {
        this.buttonAsignar = hcb;
    }

    private HtmlOutputText outputText13 = new HtmlOutputText();

    public HtmlOutputText getOutputText13() {
        return outputText13;
    }

    public void setOutputText13(HtmlOutputText hot) {
        this.outputText13 = hot;
    }

    private DropDown dropDownTipoDestinoNuevo = new DropDown();

    public DropDown getDropDownTipoDestinoNuevo() {
        return dropDownTipoDestinoNuevo;
    }

    public void setDropDownTipoDestinoNuevo(DropDown dd) {
        this.dropDownTipoDestinoNuevo = dd;
    }

    private HtmlPanelGroup groupPanel1 = new HtmlPanelGroup();

    public HtmlPanelGroup getGroupPanel1() {
        return groupPanel1;
    }

    public void setGroupPanel1(HtmlPanelGroup hpg) {
        this.groupPanel1 = hpg;
    }

    private HtmlCommandButton dataTableMatrizTDCFooterFirstButton = new HtmlCommandButton();

    public HtmlCommandButton getDataTableMatrizTDCFooterFirstButton() {
        return dataTableMatrizTDCFooterFirstButton;
    }

    public void setDataTableMatrizTDCFooterFirstButton(HtmlCommandButton hcb) {
        this.dataTableMatrizTDCFooterFirstButton = hcb;
    }

    private HtmlCommandButton dataTableMatrizTDCFooterPreviousButton = new HtmlCommandButton();

    public HtmlCommandButton getDataTableMatrizTDCFooterPreviousButton() {
        return dataTableMatrizTDCFooterPreviousButton;
    }

    public void setDataTableMatrizTDCFooterPreviousButton(HtmlCommandButton hcb) {
        this.dataTableMatrizTDCFooterPreviousButton = hcb;
    }

    private HtmlCommandButton dataTableMatrizTDCFooterNextButton = new HtmlCommandButton();

    public HtmlCommandButton getDataTableMatrizTDCFooterNextButton() {
        return dataTableMatrizTDCFooterNextButton;
    }

    public void setDataTableMatrizTDCFooterNextButton(HtmlCommandButton hcb) {
        this.dataTableMatrizTDCFooterNextButton = hcb;
    }

    private HtmlCommandButton dataTableMatrizTDCFooterLastButton = new HtmlCommandButton();

    public HtmlCommandButton getDataTableMatrizTDCFooterLastButton() {
        return dataTableMatrizTDCFooterLastButton;
    }

    public void setDataTableMatrizTDCFooterLastButton(HtmlCommandButton hcb) {
        this.dataTableMatrizTDCFooterLastButton = hcb;
    }

    private Alert alertMessage = new Alert();

    public Alert getAlertMessage() {
        return alertMessage;
    }

    public void setAlertMessage(Alert a) {
        this.alertMessage = a;
    }
    
    // </editor-fold>
    public PortletTipoDestinoPage() {
	// <editor-fold defaultstate="collapsed" desc="Creator-managed Component Initialization">
	try {
	} catch (Exception e) {
	    log("Page1 Initialization Failure", e);
	    throw e instanceof javax.faces.FacesException ? (FacesException) e: new FacesException(e);
	}
	// </editor-fold>
	// TODO: Add your own initialization code here (optional)
    }

    /** 
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected ApplicationAdminTipoDestinoBean getApplicationAdminTipoDestinoBean() {
        return (ApplicationAdminTipoDestinoBean)getBean("ApplicationAdminTipoDestinoBean");
    }


    /** 
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected SessionAdminTipoDestinoBean getSessionAdminTipoDestinoBean() {
        return (SessionAdminTipoDestinoBean)getBean("SessionAdminTipoDestinoBean");
    }


    /** 
     * <p>Return a reference to the scoped data bean.</p>
     */
    protected RequestAdminTipoDestinoBean getRequestAdminTipoDestinoBean() {
        return (RequestAdminTipoDestinoBean)getBean("RequestAdminTipoDestinoBean");
    }


    /** 
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here

        // <editor-fold defaultstate="collapsed" desc="Creator-managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("PortletTipoDestinoPage Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here

    }

    /** 
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    public void preprocess() {
    }

    /** 
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    public void prerender() {
    }

    /** 
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    public void destroy() {
    }

    /** 
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
    }


    public void dropDownCliente_processValueChange(ValueChangeEvent event) {
       AdminTipoDestinoClienteDelegate adminTDCDelegate=new AdminTipoDestinoClienteDelegate();
       String codigoCliente_=(String)this.dropDownCliente.getValue();
       if(codigoCliente_!=null && !codigoCliente_.trim().equals("")){
	   try{
	       List listTDCliente_=adminTDCDelegate.getTipoDestinoPorCliente(codigoCliente_);
	       this.getSessionAdminTipoDestinoBean().setOptionTipoDestinoCliente(this.getSessionAdminTipoDestinoBean().fillOptionTipoDestinoCliente(listTDCliente_));
	   }catch(TipoDestinoClienteException tex){
	       tex.printStackTrace();
	   }        
       }
    }


    public void dropDownDepartamento_processValueChange(ValueChangeEvent event) {
        ConsultasDelegate consDelegate=new ConsultasDelegate();
	String codigoDep_=(String)this.dropDownDepartamento.getValue();	
	if(codigoDep_!=null && !codigoDep_.trim().equals("")){
	    DepartamentoDto dto_=new DepartamentoDto();
	    dto_.setCodigoDepartamento(codigoDep_);

	    try{
		List listMunicipios_=consDelegate.getMunicipiosByDepartamento(dto_);
		this.getSessionAdminTipoDestinoBean().setOptionMunicipio(this.getSessionAdminTipoDestinoBean().fillOptionMunicipio(listMunicipios_));
	    }catch(ConsultasException cex){
		cex.printStackTrace();
	    }
	}
    }


    public void buttonConsultar_action() {
	 this.getSessionAdminTipoDestinoBean().setRenderTable(false);
	 this.getSessionAdminTipoDestinoBean().setRenderAlert(false);
         AdminTipoDestinoClienteDelegate adminTDCDelegate=new AdminTipoDestinoClienteDelegate();   
	 String codigoCliente_=(String)this.dropDownCliente.getValue();
	 String codigoTDC_=(String)this.dropDownTipodestinoCliente.getValue();
	 String codigoDep_=(String)this.dropDownDepartamento.getValue();
	 String codigoMunicipio_=(String)this.dropDownMunicipio.getValue();
	 if(codigoCliente_!=null && !codigoCliente_.trim().equals("")){
	     try{
		 List matrizTDC_=adminTDCDelegate.getMatrizTipoDestinoPorCliente(codigoCliente_, codigoTDC_, codigoDep_, codigoMunicipio_);
		 if(matrizTDC_.size()>0){
			this.getSessionAdminTipoDestinoBean().setMatrizTDC(matrizTDC_);	    
			this.getSessionAdminTipoDestinoBean().setRenderTable(true);
		 }else{
			this.setMensajeAlert("warning", "Advertencia",AdminTipoDestinoSource.getInstance().getAdminTipoDestinoErrorSetupKey("NoExistenRegistros"), true);
			this.getSessionAdminTipoDestinoBean().setRenderAlert(true);
		 }
	     }catch(TipoDestinoClienteException tex){
		this.setMensajeAlert("error", "Error",AdminTipoDestinoSource.getInstance().getAdminTipoDestinoErrorSetupKey("ErrorSistema"), true);
		this.getSessionAdminTipoDestinoBean().setRenderAlert(true);
		tex.printStackTrace();
	     }           
	 }else{
	     this.setMensajeAlert("warning", "Advertencia",AdminTipoDestinoSource.getInstance().getAdminTipoDestinoErrorSetupKey("WarningCliente"), true);
		this.getSessionAdminTipoDestinoBean().setRenderAlert(true);
	 }
    }


    public void buttonAsignar_action() {
	this.getSessionAdminTipoDestinoBean().setRenderAlert(false);
        List listCheck=this.checkboxTDC.getSelected("checkSelectionGroup");
	String codigoTDNuevo_=(String)this.dropDownTipoDestinoNuevo.getValue();	
	String codigoCliente_="";
	List listAsignaciones_=new ArrayList();
	if(listCheck!=null && listCheck.size()>0){
	    for(Iterator it=listCheck.iterator();it.hasNext();){
		String codigos_=(String)it.next();
		String[] arrayCodigos=codigos_.split("-");

		List asig_=new ArrayList();

		String codigoTDC_=arrayCodigos[0];
		codigoCliente_=arrayCodigos[1];
		String codigoDep_=arrayCodigos[2];
		String codigoMun_=arrayCodigos[3];
		String codigoCentroP_=arrayCodigos[4];	    
		asig_.add(codigoTDC_);
		asig_.add(codigoDep_);
		asig_.add(codigoMun_);
		asig_.add(codigoCentroP_);
		listAsignaciones_.add(asig_);
	    }
	    AdminTipoDestinoClienteDelegate adminTDCDelegate=new AdminTipoDestinoClienteDelegate();
	    String ldapUser_=this.getSessionAdminTipoDestinoBean().getLdapUser();
	    try{
		adminTDCDelegate.updateMatrizTipoDestinoCliente(codigoCliente_, listAsignaciones_, codigoTDNuevo_,ldapUser_);
		this.buttonConsultar_action();
	    }catch(TipoDestinoClienteException tex){
		this.setMensajeAlert("error", "Error",AdminTipoDestinoSource.getInstance().getAdminTipoDestinoErrorSetupKey("ErrorSistema"), true);
		this.getSessionAdminTipoDestinoBean().setRenderAlert(true);
		tex.printStackTrace();
	    }
	}
    }


    public String dataTableMatrizTDC_firstPageAction() {
        dataTableMatrizTDC.setFirst(0);
        return null;
    }


    public String dataTableMatrizTDC_previousPageAction() {
        int first = dataTableMatrizTDC.getFirst() - dataTableMatrizTDC.getRows();
        if (first < 0) {
            first = 0;
        }
        dataTableMatrizTDC.setFirst(first);
        return null;
    }


    public String dataTableMatrizTDC_nextPageAction() {
        int first = dataTableMatrizTDC.getFirst() + dataTableMatrizTDC.getRows();
        dataTableMatrizTDC.setRowIndex(first);
        if (dataTableMatrizTDC.isRowAvailable()) {
            dataTableMatrizTDC.setFirst(first);
        }
        return null;
    }


    public String dataTableMatrizTDC_lastPageAction() {
        int first = dataTableMatrizTDC.getFirst();
        while (true) {
            dataTableMatrizTDC.setRowIndex(first + 1);
            if (dataTableMatrizTDC.isRowAvailable()) {
                first++;
            } else {
                break;
            }
        }
        dataTableMatrizTDC.setFirst(first - (first % dataTableMatrizTDC.getRows()));
        return null;
    }
    
    public void setMensajeAlert(String type, String summary, String detail, boolean visible) 
    {
        if (visible){
            this.alertMessage.setType(type);
            this.alertMessage.setSummary(summary);
            this.alertMessage.setDetail(detail);
            this.alertMessage.setVisible(visible);
        }else{
            this.alertMessage.setVisible(visible);
        }
    }
}
